package com.example.demo.dao;

import com.example.demo.model.Emp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.*;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@Slf4j
public class EmpDao {

    @Autowired
    @Qualifier("empTemplate")
    JdbcTemplate jdbcTemplate;


    public void empOpt() {
        jdbcTemplate.execute("create table t_emp(id int identity ,title varchar(255))");
        jdbcTemplate.update("insert into t_emp(title) values('Rose')");
        jdbcTemplate.queryForList("select * from t_emp").forEach(m->log.info(m.toString()));
        log.info(jdbcTemplate.queryForMap("select * from t_emp where id=1").toString());
        log.info(jdbcTemplate.queryForObject("select title from t_emp where id=1",String.class));
    }



    public void save() {
//        Emp emp = new Emp();
        Emp emp = new Emp(null, "Rose", "clerk", 9876D);
        jdbcTemplate.update("insert into t_emp(name,job,salary) values(?,?,?)",
                new Object[]{"Rose", "clerk", 9876D});
        log.info(emp.toString());

    }
    public void findAll() {
        RowMapper<Emp> mapper = BeanPropertyRowMapper.newInstance(Emp.class);
        jdbcTemplate.query("select * from t_emp",mapper).forEach(e->log.info(e.toString()));

    }


}
